/// \file triang.cpp
/// \brief refinement rules
/// \author LNM RWTH Aachen: Sven Gross, Joerg Peters, Volker Reichelt; SC RWTH Aachen:

/*
 * This file is part of DROPS.
 *
 * DROPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DROPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DROPS. If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Copyright 2009 LNM/SC RWTH Aachen, Germany
*/

/// alternative to refinement rules from topo.cpp, is used in maketopo.cpp

#ifdef DROPS_WIN
#  include "misc/utils.h"
#  include "geom/topo.h"
#endif

struct RawRuleCL
{
    int ChildNum;
    int Children[8][4];
};

// Vertices of a given child;
// A tetrahedron can have 94 different children (including itself, that is),
// which are listed here in the following order:
// 1) The first eight tetrahedrons are those of the regular refinement-rule
//    in lexicographical order.
// 2) Then follow all other, also in lexicographical order.
const DROPS::Ubyte VertOfChildAr[DROPS::NumAllChildrenC][DROPS::NumVertsC]= {
    {0, 4, 5, 7}, {4, 1, 6, 8}, {4, 5, 6, 8}, {4, 5, 7, 8}, {5, 6, 2, 9}, {5, 6, 8, 9},
    {5, 7, 8, 9}, {7, 8, 9, 3},
    {0, 1, 2, 3}, {0, 1, 2, 7}, {0, 1, 2, 8}, {0, 1, 2, 9}, {0, 1, 5, 3}, {0, 1, 5, 7},
    {0, 1, 5, 8}, {0, 1, 6, 3}, {0, 1, 6, 7}, {0, 1, 6, 8}, {0, 1, 9, 3}, {0, 2, 8, 3},
    {0, 2, 8, 9}, {0, 4, 2, 3}, {0, 4, 2, 7}, {0, 4, 2, 9}, {0, 4, 5, 3}, {0, 4, 9, 3},
    {0, 5, 8, 3}, {0, 6, 2, 3}, {0, 6, 2, 7}, {0, 6, 2, 9}, {0, 6, 8, 3}, {0, 6, 8, 9},
    {0, 6, 9, 3}, {0, 8, 9, 3}, {1, 2, 7, 3}, {1, 2, 7, 8}, {1, 2, 7, 9}, {1, 5, 2, 3},
    {1, 5, 2, 8}, {1, 5, 2, 9}, {1, 5, 6, 3}, {1, 5, 6, 7}, {1, 5, 6, 8}, {1, 5, 7, 3},
    {1, 5, 7, 8}, {1, 5, 7, 9}, {1, 5, 9, 3}, {1, 6, 7, 3}, {1, 6, 7, 8}, {1, 7, 9, 3},
    {2, 7, 8, 3}, {2, 7, 8, 9}, {4, 1, 2, 3}, {4, 1, 2, 8}, {4, 1, 2, 9}, {4, 1, 6, 3},
    {4, 1, 9, 3}, {4, 2, 7, 3}, {4, 2, 7, 8}, {4, 2, 7, 9}, {4, 2, 8, 3}, {4, 2, 8, 9},
    {4, 5, 2, 3}, {4, 5, 2, 8}, {4, 5, 2, 9}, {4, 5, 6, 3}, {4, 5, 6, 9}, {4, 5, 7, 3},
    {4, 5, 7, 9}, {4, 5, 8, 3}, {4, 5, 9, 3}, {4, 6, 2, 3}, {4, 6, 2, 7}, {4, 6, 2, 9},
    {4, 6, 7, 8}, {4, 6, 8, 3}, {4, 6, 8, 9}, {4, 6, 9, 3}, {4, 7, 8, 9}, {4, 7, 9, 3},
    {4, 8, 9, 3}, {5, 2, 8, 3}, {5, 2, 8, 9}, {5, 6, 2, 3}, {5, 6, 7, 9}, {5, 6, 8, 3},
    {5, 6, 9, 3}, {5, 7, 8, 3}, {5, 8, 9, 3}, {6, 2, 7, 3}, {6, 2, 7, 9}, {6, 7, 8, 3},
    {6, 7, 8, 9}, {6, 7, 9, 3} };

RawRuleCL RawRules[64]= {
    {1, {{0, 1, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {2, {{0, 4, 2, 3}, {4, 1, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {2, {{0, 1, 5, 3}, {1, 5, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 4, 5, 3}, {4, 1, 2, 3}, {4, 5, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {2, {{0, 1, 6, 3}, {0, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 4, 2, 3}, {4, 1, 6, 3}, {4, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 5, 3}, {1, 5, 6, 3}, {5, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 5, 3}, {4, 1, 6, 3}, {4, 5, 6, 3}, {5, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {2, {{0, 1, 2, 7}, {1, 2, 7, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 4, 2, 7}, {4, 1, 2, 3}, {4, 2, 7, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 5, 7}, {1, 5, 2, 3}, {1, 5, 7, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 5, 7}, {4, 1, 2, 3}, {4, 5, 2, 3}, {4, 5, 7, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 6, 7}, {0, 6, 2, 7}, {1, 6, 7, 3}, {6, 2, 7, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 2, 7}, {4, 1, 6, 3}, {4, 2, 7, 3}, {4, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 5, 7}, {1, 5, 6, 3}, {1, 5, 7, 3}, {5, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 5, 7}, {4, 1, 6, 3}, {4, 5, 6, 3}, {4, 5, 7, 3}, {5, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {2, {{0, 1, 2, 8}, {0, 2, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 4, 2, 3}, {4, 1, 2, 8}, {4, 2, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 5, 8}, {0, 5, 8, 3}, {1, 5, 2, 8}, {5, 2, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 5, 3}, {4, 1, 2, 8}, {4, 2, 8, 3}, {4, 5, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 6, 8}, {0, 6, 2, 3}, {0, 6, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 2, 3}, {4, 1, 6, 8}, {4, 6, 2, 3}, {4, 6, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 1, 5, 8}, {0, 5, 8, 3}, {1, 5, 6, 8}, {5, 6, 2, 3}, {5, 6, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 5, 3}, {4, 1, 6, 8}, {4, 5, 6, 3}, {4, 6, 8, 3}, {5, 6, 2, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 2, 7}, {1, 2, 7, 8}, {2, 7, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 2, 7}, {2, 7, 8, 3}, {4, 1, 2, 8}, {4, 2, 7, 8}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 1, 5, 7}, {1, 5, 2, 8}, {1, 5, 7, 8}, {5, 2, 8, 3}, {5, 7, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 4, 5, 7}, {4, 1, 2, 8}, {4, 5, 2, 8}, {4, 5, 7, 8}, {5, 2, 8, 3}, {5, 7, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 1, 6, 7}, {0, 6, 2, 7}, {1, 6, 7, 8}, {6, 2, 7, 3}, {6, 7, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 4, 2, 7}, {4, 1, 6, 8}, {4, 6, 2, 7}, {4, 6, 7, 8}, {6, 2, 7, 3}, {6, 7, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 1, 5, 7}, {1, 5, 6, 8}, {1, 5, 7, 8}, {5, 6, 2, 3}, {5, 6, 8, 3}, {5, 7, 8, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {7, {{0, 4, 5, 7}, {4, 1, 6, 8}, {4, 5, 6, 8}, {4, 5, 7, 8}, {5, 6, 2, 3}, {5, 6, 8, 3}, {5, 7, 8, 3}, {-1, -1, -1, -1}} },
    {2, {{0, 1, 2, 9}, {0, 1, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 4, 2, 9}, {0, 4, 9, 3}, {4, 1, 2, 9}, {4, 1, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 5, 3}, {1, 5, 2, 9}, {1, 5, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 5, 3}, {4, 1, 2, 9}, {4, 1, 9, 3}, {4, 5, 2, 9}, {4, 5, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 6, 3}, {0, 6, 2, 9}, {0, 6, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 2, 9}, {0, 4, 9, 3}, {4, 1, 6, 3}, {4, 6, 2, 9}, {4, 6, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 5, 3}, {1, 5, 6, 3}, {5, 6, 2, 9}, {5, 6, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 5, 3}, {4, 1, 6, 3}, {4, 5, 6, 3}, {5, 6, 2, 9}, {5, 6, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 2, 7}, {1, 2, 7, 9}, {1, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 2, 7}, {4, 1, 2, 9}, {4, 1, 9, 3}, {4, 2, 7, 9}, {4, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 5, 7}, {1, 5, 2, 9}, {1, 5, 7, 9}, {1, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 4, 5, 7}, {4, 1, 2, 9}, {4, 1, 9, 3}, {4, 5, 2, 9}, {4, 5, 7, 9}, {4, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 1, 6, 7}, {0, 6, 2, 7}, {1, 6, 7, 3}, {6, 2, 7, 9}, {6, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 4, 2, 7}, {4, 1, 6, 3}, {4, 2, 7, 9}, {4, 6, 2, 9}, {4, 6, 9, 3}, {4, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 1, 5, 7}, {1, 5, 6, 7}, {1, 6, 7, 3}, {5, 6, 2, 9}, {5, 6, 7, 9}, {6, 7, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {7, {{0, 4, 5, 7}, {4, 1, 6, 3}, {4, 5, 6, 9}, {4, 5, 7, 9}, {4, 6, 9, 3}, {4, 7, 9, 3}, {5, 6, 2, 9}, {-1, -1, -1, -1}} },
    {3, {{0, 1, 2, 8}, {0, 2, 8, 9}, {0, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 2, 9}, {0, 4, 9, 3}, {4, 1, 2, 8}, {4, 2, 8, 9}, {4, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 1, 5, 8}, {0, 5, 8, 3}, {1, 5, 2, 8}, {5, 2, 8, 9}, {5, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 4, 5, 3}, {4, 1, 2, 8}, {4, 5, 2, 8}, {4, 5, 8, 3}, {5, 2, 8, 9}, {5, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 6, 8}, {0, 6, 2, 9}, {0, 6, 8, 9}, {0, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 4, 2, 9}, {0, 4, 9, 3}, {4, 1, 6, 8}, {4, 6, 2, 9}, {4, 6, 8, 9}, {4, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 1, 5, 8}, {0, 5, 8, 3}, {1, 5, 6, 8}, {5, 6, 2, 9}, {5, 6, 8, 9}, {5, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {7, {{0, 4, 5, 3}, {4, 1, 6, 8}, {4, 5, 6, 8}, {4, 5, 8, 3}, {5, 6, 2, 9}, {5, 6, 8, 9}, {5, 8, 9, 3}, {-1, -1, -1, -1}} },
    {4, {{0, 1, 2, 7}, {1, 2, 7, 8}, {2, 7, 8, 9}, {7, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {5, {{0, 4, 2, 7}, {2, 7, 8, 9}, {4, 1, 2, 8}, {4, 2, 7, 8}, {7, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {6, {{0, 1, 5, 7}, {1, 5, 2, 8}, {1, 5, 7, 8}, {5, 2, 8, 9}, {5, 7, 8, 9}, {7, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {7, {{0, 4, 5, 7}, {4, 1, 2, 8}, {4, 5, 2, 8}, {4, 5, 7, 8}, {5, 2, 8, 9}, {5, 7, 8, 9}, {7, 8, 9, 3}, {-1, -1, -1, -1}} },
    {6, {{0, 1, 6, 7}, {0, 6, 2, 7}, {1, 6, 7, 8}, {6, 2, 7, 9}, {6, 7, 8, 9}, {7, 8, 9, 3}, {-1, -1, -1, -1}, {-1, -1, -1, -1}} },
    {7, {{0, 4, 2, 7}, {4, 1, 6, 8}, {4, 2, 7, 9}, {4, 6, 2, 9}, {4, 6, 8, 9}, {4, 7, 8, 9}, {7, 8, 9, 3}, {-1, -1, -1, -1}} },
    {7, {{0, 1, 5, 7}, {1, 5, 6, 8}, {1, 5, 7, 8}, {5, 6, 2, 9}, {5, 6, 8, 9}, {5, 7, 8, 9}, {7, 8, 9, 3}, {-1, -1, -1, -1}} },
    {8, {{0, 4, 5, 7}, {4, 1, 6, 8}, {4, 5, 6, 8}, {4, 5, 7, 8}, {5, 6, 2, 9}, {5, 6, 8, 9}, {5, 7, 8, 9}, {7, 8, 9, 3}} }
    };
